﻿using System;
using System.Data;
using System.Linq;
using System.Windows.Forms;
using Telerik.WinControls.UI;
using Zvezda.FSO2.DB;

namespace FSO
{
    public partial class UserControlSearchCalendarPlan : UserControl, InterfaceSearchControl
    {
        private bool _openOnDblClick = false;

        private bool _isNotFill = true;

        public UserControlSearchCalendarPlan()
        {
            InitializeComponent();
            
        }
        public void InitializeGridView()
        {
            GridViewTextBoxColumn colNumber = new GridViewTextBoxColumn(dataSetFSO1.View_SEARCH_CALENDAR_PLAN.NUMBERColumn.ColumnName);
            colNumber.HeaderText = "№";

            GridViewDateTimeColumn colStartDate = new GridViewDateTimeColumn(dataSetFSO1.View_SEARCH_CALENDAR_PLAN.START_DATEColumn.ColumnName);
            colStartDate.HeaderText = "Дата начала";
            colStartDate.FormatString = "{0:d}";

            GridViewDateTimeColumn colEndDate = new GridViewDateTimeColumn(dataSetFSO1.View_SEARCH_CALENDAR_PLAN.END_DATEColumn.ColumnName);
            colEndDate.HeaderText = "Дата окончания";
            colEndDate.FormatString = "{0:d}";

            GridViewTextBoxColumn colContractNumber = new GridViewTextBoxColumn(dataSetFSO1.View_SEARCH_CALENDAR_PLAN.CONTRACT_NUMBERColumn.ColumnName);
            colContractNumber.HeaderText = "Договор";

            GridViewTextBoxColumn colOrgName = new GridViewTextBoxColumn(dataSetFSO1.View_SEARCH_CALENDAR_PLAN.NSI_ORG_NAMEColumn.ColumnName);
            colOrgName.HeaderText = "Организация";

            GridViewTextBoxColumn colGroups = new GridViewTextBoxColumn(dataSetFSO1.View_SEARCH_CALENDAR_PLAN.PROD_GROUPSColumn.ColumnName);
            colGroups.HeaderText = "Группы товаров";

            radGridViewSearchCalendarPlan.MasterTemplate.Columns.Clear();
            radGridViewSearchCalendarPlan.MasterTemplate.Columns.Add(colNumber);
            radGridViewSearchCalendarPlan.MasterTemplate.Columns.Add(colStartDate);
            radGridViewSearchCalendarPlan.MasterTemplate.Columns.Add(colEndDate);
            radGridViewSearchCalendarPlan.MasterTemplate.Columns.Add(colContractNumber);
            radGridViewSearchCalendarPlan.MasterTemplate.Columns.Add(colOrgName);
            radGridViewSearchCalendarPlan.MasterTemplate.Columns.Add(colGroups);
            radGridViewSearchCalendarPlan.DataSource = dataSetFSO1.View_SEARCH_CALENDAR_PLAN;
            
        }

        #region InterfaceSearchControl members

        public bool OpenOnDoubleClick
        {
            get
            {
                return _openOnDblClick;
            }
            set
            {
                _openOnDblClick = value;
            }
        }

        public bool EnableFiltering
        {
            get
            {
                return radGridViewSearchCalendarPlan.EnableFiltering;
            }
            set
            {
                radGridViewSearchCalendarPlan.EnableFiltering = value;
            }
        }

        public DataRow SelectedRow
        {
            get
            {
                DataSetFSO.View_SEARCH_CALENDAR_PLANRow dr;
                DataRow returnRow;
                if (radGridViewSearchCalendarPlan.SelectedRows.Count > 0)
                {
                    dr = (DataSetFSO.View_SEARCH_CALENDAR_PLANRow)(((DataRowView)(radGridViewSearchCalendarPlan.SelectedRows.First()).DataBoundItem)).Row;
                    returnRow = calendaR_PLANTableAdapter1.GetDataById(dr.ID).FindByID(dr.ID);
                }
                else
                {
                    returnRow = null;
                }
                return returnRow;
            }
        }

        public GridViewSelectedRowsCollection SelectedRows
        {
            get
            {
                return radGridViewSearchCalendarPlan.SelectedRows;
            }
        }

        public DataSet OwnerDataSet
        {
            set { _isNotFill = false; dataSetFSO1 = (DataSetFSO)value; InitializeGridView(); }
            get { return dataSetFSO1; }
        }

        public void RefreshList()
        {
            view_SEARCH_CALENDAR_PLANTableAdapter1.Fill(dataSetFSO1.View_SEARCH_CALENDAR_PLAN);
        }

        #endregion

        #region Events

        private void UserControlSearchCalendarPlan_Load(object sender, EventArgs e)
        {
            if (_isNotFill)
            {
                view_SEARCH_CALENDAR_PLANTableAdapter1.Fill(dataSetFSO1.View_SEARCH_CALENDAR_PLAN);
               
            }

            InitializeGridView();
        }

        private void radGridViewSearchCalendarPlan_CellDoubleClick(object sender, GridViewCellEventArgs e)
        {
            if (this.SelectedRow != null && _openOnDblClick)
            {
                FormCalendarPlan formCalendarPlan = new FormCalendarPlan((DataSetFSO.CALENDAR_PLANRow)this.SelectedRow);

                if (!FormHelper.HasOpenedForm(formCalendarPlan.FormName))
                {
                    formCalendarPlan.MdiParent = FormHelper.MainForm;
                    formCalendarPlan.Show();
                    formCalendarPlan.Select();
                }
            }
        }

        #endregion
    }
}
